<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../rest.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .main {
            position: absolute;
            left: 300px;
        }

        .nav {
            width: 800px;
            height: 40px;
            display: flex;
            text-align: center;
            list-style: none;
        }

        .nav li {
            flex: 1;
            border: 1px solid black;
            line-height: 40px;
        }

        .nav li+li {
            border-left: 0;
        }

        .img div {
            width: 800px;
            display: none;
            position: absolute;
        }

        .img div img {
            width: 100%;
        }

        .img .imgActive {
            display: block;
        }

        .main .nav .liActive {
            background: aqua;
        }
    </style>
</head>

<body>
    <div class="main">
        <ul class="nav">
            <li class="liActive">最近更新</li>
            <li>周一</li>
            <li>周二</li>
            <li>周三</li>
            <li>周四</li>
            <li>周五</li>
            <li>周六</li>
            <li>周日</li>
        </ul>
        <div class="img">
            <div class="inactive imgActive "><img src="./images/1.webp" alt=""></div>
            <div class="inactive"><img src="./images/2.webp" alt=""></div>
            <div class="inactive"><img src="./images/3.webp" alt=""></div>
            <div class="inactive"><img src="./images/4.webp" alt=""></div>
            <div class="inactive"><img src="./images/5.webp" alt=""></div>
            <div class="inactive"><img src="./images/6.webp" alt=""></div>
            <div class="inactive"><img src="./images/7.webp" alt=""></div>
            <div class="inactive"><img src="./images/8.webp" alt=""></div>
        </div>
    </div>


</body>
<script>

    var liList = Array.from(document.querySelectorAll('.nav li'))
    var imgList = Array.from(document.querySelectorAll('.inactive'))
    var wrap = document.querySelector('.img')
    var liNum = 0
    var timer = null

    liList.forEach(function (li, index) {
        li.onclick = function () {
            //清除全部
            for (i = 0; i < liList.length; i++) {
                liList[i].className = ''
                imgList[i].className = 'inactive'
            }
            //把点击的元素设为活跃状态
            imgList[index].className = 'inactive imgActive'
            this.className = 'liActive'
            liNum = index
            setTimer()
        }
    })
    /*--------------------------*/
    // 轮播部分
    setTimer()
    //移入图片暂停计时
    wrap.onmouseover = function () {
        clearInterval(timer)
    }
    //移出图片重新开始计时
    wrap.onmouseout = function () {
        setTimer()
    }
    //封装轮播函数
    function setTimer() {
        //清除上一个计时器
        clearInterval(timer)
        timer = setInterval(function () {
            liNum++
            if (liNum >= liList.length) {
                liNum = 0
            }
            for (i = 0; i < liList.length; i++) {
                liList[i].className = ''
                imgList[i].className = 'inactive'
            }
            liList[liNum].className = 'liActive'
            imgList[liNum].className = 'inactive imgActive'
            console.log(liNum)
        }, 3000)
    }
</script>

</html>